package com.mall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.strings.SuccessMsg;
import com.mall.entity.TbSpecOption;
import com.mall.entity.TbSpeclification;
import com.mall.product.mapper.SpecOptionMapper;
import com.mall.product.mapper.SpeclificationMapper;
import com.mall.product.service.ISpeclificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * impl speclification服务
 * created with IntelliJ IDEA
 *
 * @author ASUS
 * @Auther: W
 * @Date: 2021/07/16/14:57
 * Other:
 * @date 2021/07/18
 */
@Service
public class SpeclificationServiceImpl implements ISpeclificationService {

    @Autowired
    private SpeclificationMapper speclificationMapper;

    @Autowired
    private SpecOptionMapper specOptionMapper;

    @Override
    public List<TbSpeclification> findAll() {
        return speclificationMapper.findAll();
    }

    @Override
    public List<Map> findList(String name) {
        List<TbSpeclification> list = speclificationMapper.findList(name);
        List<Map> result = new ArrayList<>();
        list.stream().forEach(spec ->{
            List<TbSpecOption> specOptionList = specOptionMapper.findById(spec.getId());
            Map<String, Object> map = new HashMap();
            map.put("spec",spec);
            map.put("specOptionList",specOptionList);
            result.add(map);
        });
        return result;
    }

    @Override
    public boolean addSpec(TbSpeclification speclification) {
        TbSpeclification one = speclificationMapper.findOne(speclification.getSpecName());
        if (one != null) {
            // 存在相同属性  进行更改编辑
            return false;
        }
        try {
            speclificationMapper.addSpec(speclification);

            List<String> options = Arrays.asList(speclification.getOptions().split(","));
            options.stream().forEach(optionName ->{
                TbSpecOption tbSpecOption = new TbSpecOption();
                tbSpecOption.setSpecId(speclification.getId());
                tbSpecOption.setOptionName(optionName);
                specOptionMapper.add(tbSpecOption);
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public TbSpeclification findOne(String name) {
        return speclificationMapper.findOne(name);
    }

    @Override
    public List<Map> findByid(Integer id) {
        List<TbSpeclification> list = speclificationMapper.findByid(id);
        List<Map> add = new ArrayList<>();
        list.stream().forEach(spec ->{
            List<TbSpecOption> specOptionList = specOptionMapper.findById(spec.getId());
            Map<String, Object> map = new HashMap();
            map.put("spec",spec);
            map.put("specOptionList",specOptionList);
            add.add(map);
        });

        return add;
    }

    @Override
    public CommonResponse deleteById(Integer id) {
        //删除对应规格属性
        specOptionMapper.deleteByLificationId(id);
        //删除规格信息
        speclificationMapper.deleteById(id);
        return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
    }

    @Override
    public CommonResponse batchDelete(Integer[] ids) {
        for (Integer id : ids) {
            //删除对应规格属性
            specOptionMapper.deleteByLificationId(id);
            //删除规格信息
            speclificationMapper.deleteById(id);
        }
        return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
    }

    @Override
    public CommonResponse update(TbSpeclification speclification) {
        //根据id修改规格信息
        speclificationMapper.update(speclification);
        //根据规格id删除规格属性
        specOptionMapper.deleteByLificationId(speclification.getId());
        //然后遍历新的规格属性进行添加
        List<String> options = Arrays.asList(speclification.getOptions().split(","));
        options.stream().forEach(optionName ->{
            TbSpecOption tbSpecOption = new TbSpecOption();
            tbSpecOption.setSpecId(speclification.getId());
            tbSpecOption.setOptionName(optionName);
            specOptionMapper.add(tbSpecOption);
        });
        return new CommonResponse(CommonStatus.SUCCESS.getCode(), SuccessMsg.SUCCESS_CODE_200);
    }
}
